$().ready(() => {
  let id = window.location.search.split("?id=").join('')
  let detailData = Interceptor('/detailPage', 'get', {
    'id': id
  }).data

  $('.poll-countdown').text('投票结束时间：' + detailData.end_time);
  $('.poll-title').text(detailData.name);
  $('.poll-info img').attr('src', detailData.img);
  $('.poll-message').text(detailData.content);
  $('.poll-user').text('项目发起人：' + detailData.userName);
  $('.poll-btn').text('可投' + detailData.poll_number + '票');

  let curDate = new Date();
  let endTime = new Date((detailData.end_time).substring(0, 19).replace(/-/g, '/'));

  if (curDate < endTime){
    for (let i = 0; i < detailData.optMsg.length; i++) {
      let pollList = `
        <div class="poll-list" num="${detailData.optMsg[i]['optId']}">
        <span>${detailData.optMsg[i]['content']}</span>
        <div class="button">投票</div>
        </div>`
      $('.poll-ul').append(pollList)
    }

    $('.button').click(function (e){
      if ($(e.target).attr('class') === 'button'){
        let userThrow = Interceptor('/userThrow','post',{
          'id':id,
          'optId': $(e.target).parent().attr('num')
        })
        if (userThrow.code === 200){
          $(e.target).html('投票成功').attr('class','btn-end');
        }else {
          toolTip(0,'票数不够')
        }
      }
    })
  }else {
    for (let i = 0; i < detailData.optMsg.length; i++) {
      let pollList = `
        <div class="poll-list" num="${detailData.optMsg[i]['optId']}">
        <span>${detailData.optMsg[i]['content']}</span>
        <div>${detailData.optMsg[i]['count']} 票</div>
        </div>`
      $('.poll-ul').append(pollList);
      $('.poll-btn').text('已结束').css('background','#666666')
    }
  }


})